package com.tss.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tss.mapper.RecordMapper;
import com.tss.pojo.Record;
import com.tss.pojo.RecordExample;
import com.tss.pojo.RecordExample.Criteria;
import com.tss.pojo.Result;
import com.tss.service.RecordService;

@Service
public class RecordServiceImpl implements RecordService{
	
	@Autowired
	private RecordMapper recordMapper;
	
	@Override
	public Result addRecord(Record record) {
		Result result = new Result();
		if(record==null||record.getAttendanceId()==null||record.getStudentId()==null){
			result.setErrorMessage("参数错误！");
			return result;
		}
		
		RecordExample example = new RecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andAttendanceIdEqualTo(record.getAttendanceId());
		criteria.andStudentIdEqualTo(record.getStudentId());
		criteria.andValidateEqualTo(true);
		List<Record> r = recordMapper.selectByExample(example);
		if(r==null||r.isEmpty()){
			record.setValidate(true);
			recordMapper.insert(record);
			result.setSuccessMessage("添加记录成功！");
			return result;
		}
		
		return result.setErrorMessage("不要重复签到！");
	}

	@Override
	public Long countRecord(Long attendanceId) {
		if(attendanceId==null){
			return 0L;
		}
		RecordExample example = new RecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		criteria.andAttendanceIdEqualTo(attendanceId);
		return (long) recordMapper.countByExample(example);
	}
	
	

}
